package avicit.bdp.dds.server.worker.task.monitor;


import avicit.bdp.common.utils.SpringApplicationContext;
import avicit.bdp.dds.dispatch.task.AbstractParameters;
import avicit.bdp.dds.dispatch.task.monitordir.MonitorDirectoryParameters;
import avicit.bdp.dds.common.Constants;
import avicit.bdp.dds.server.entity.TaskExecutionContext;
import avicit.bdp.dds.server.worker.task.AbstractTask;
import avicit.bdp.dds.service.process.ProcessService;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;


/**
 * 目录监控节点
 */
public class MonitorDirectoryTask extends AbstractTask {

    /**
     * 目录监控参数
     */
    private MonitorDirectoryParameters monitorDirectoryParameters;


    /**
     * taskExecutionContext
     */
    private TaskExecutionContext taskExecutionContext;

    private final ProcessService processService = SpringApplicationContext.getBean(ProcessService.class);

    /**
     * constructor
     *
     * @param taskExecutionContext taskExecutionContext
     * @param logger               logger
     */
    public MonitorDirectoryTask(TaskExecutionContext taskExecutionContext, Logger logger) {
        super(taskExecutionContext, logger);
        this.taskExecutionContext = taskExecutionContext;
    }

    @Override
    public void init() {
        logger.info("table task params {}", taskExecutionContext.getTaskParams());
        this.monitorDirectoryParameters = JSONObject.parseObject(taskExecutionContext.getTaskParams(), MonitorDirectoryParameters.class);

        if (!monitorDirectoryParameters.checkParameters()) {
            throw new RuntimeException("http task params is not valid");
        }
    }

    @Override
    public void handle() throws Exception {
        //TODO 处理文件资源设置流程ID

        //设置执行成功状态
        setExitStatusCode(Constants.EXIT_CODE_SUCCESS);
    }





    @Override
    public AbstractParameters getParameters() {
        return this.monitorDirectoryParameters;
    }
}
